/**
 * 
 */
package org.xlab.baseframework.test.platform.webservice.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.exception.ObjectNotFoundException;
import org.xlab.baseframework.exception.SystemException;
import org.xlab.baseframework.test.platform.domain.BizTransactionLog;
import org.xlab.baseframework.test.platform.domain.EpcCache;
import org.xlab.baseframework.test.platform.domain.SystemSession;
import org.xlab.baseframework.test.platform.domain.enumerate.SynchronizedInditify;
import org.xlab.baseframework.test.platform.service.api.IBizTransactionLogService;
import org.xlab.baseframework.test.platform.service.api.ISystemSessionService;
import org.xlab.baseframework.test.platform.webservice.api.IPalletAssociationWebService;

/**
 * @author makubex
 * 
 */
@WebService(endpointInterface = "org.xlab.baseframework.test.platform.webservice.api.IPalletAssociationWebService")
public class PalletAssociationWebServiceImpl implements
		IPalletAssociationWebService {

	private IBizTransactionLogService bizTransactionLogService;
	private ISystemSessionService systemSessionService;

	/**
	 * @return the bizTransactionLogService
	 */
	public IBizTransactionLogService getBizTransactionLogService() {
		return bizTransactionLogService;
	}

	/**
	 * @return the systemSessionService
	 */
	public ISystemSessionService getSystemSessionService() {
		return systemSessionService;
	}

	/**
	 * @see org.xlab.baseframework.test.platform.webservice.api.IPalletAssociationWebService#pa(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public void pa(String epcNo, String location, Integer qty, String lotNo,
			String sessionId, String deviceId) throws SystemException {
		SystemSession exampleInstance = new SystemSession();
		exampleInstance.setDeletedInd(EnumDeletedIndentifying.N);
		exampleInstance.setDeviceId(deviceId);
		exampleInstance.setSessionId(sessionId);
		try {
			List<SystemSession> allSession = systemSessionService
					.findAllByExample(exampleInstance);
			SystemSession session = allSession.get(0);
			session.setLastUpdatedTime(new Date());
			systemSessionService.update(session);
			BizTransactionLog bizLog = new BizTransactionLog();
			bizLog.setCommandId("PA");
			bizLog.setCreatedBy(session.getCreatedBy());
			bizLog.setCreatedTime(session.getLastUpdatedTime());
			bizLog.setLastUpdatedBy(session.getCreatedBy());
			bizLog.setLastUpdatedTime(session.getLastUpdatedTime());
			bizLog.setSessionId(session);
			bizLog.setDeviceId(deviceId);
			bizLog.setSynchronizedInd(SynchronizedInditify.N);
			bizLog.setEpcNo(epcNo);
			bizLog.setLocation(location);
			bizLog.setToken(new Timestamp(session.getLastUpdatedTime()
					.getTime()).toString());
			bizLog.setLotNo(lotNo);
			bizLog.setQty(qty);
			this.bizTransactionLogService.save(bizLog);
			EpcCache.getInstance().saveOrUpdateEpcInfo(epcNo, location,
					bizLog.getCommandId(),
					session.getCreatedBy(), session.getLastUpdatedTime());
		} catch (ObjectNotFoundException e) {
			throw new SystemException(e);
		}

	}

	/**
	 * @param bizTransactionLogService
	 *            the bizTransactionLogService to set
	 */
	public void setBizTransactionLogService(
			IBizTransactionLogService bizTransactionLogService) {
		this.bizTransactionLogService = bizTransactionLogService;
	}

	/**
	 * @param systemSessionService
	 *            the systemSessionService to set
	 */
	public void setSystemSessionService(
			ISystemSessionService systemSessionService) {
		this.systemSessionService = systemSessionService;
	}

}
